//**********************************************************
//    File Name: FrmViewClient.cs
//    Author: Team Motif
//    Date Created : 04/13/2007
//    Current Owner: Team Motif 
//                                                                            
//    Purpose: 
//    To view and update the Client specific data such as profile, chart data, assessment data, and appointments.
//
//	  Copyright 2007
//**********************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using Library;

namespace Elderhealth
{

    public partial class FrmViewClient : Form
    {
        /// <summary>
        /// Default constructor.  Assumes creating new client
        /// </summary>
        public FrmViewClient()
        {
            InitializeComponent();
            mClient = new Client();

            mIsCreatingNewClient = true;
            this.mSaveButton.Visible = false;
            this.mCreateButton.Visible = true;

        }

        /// <summary>
        /// Constructor, with an existing client object
        /// </summary>
        /// <param name="client"></param>
        public FrmViewClient(Client client)
        {
            mClient = client;
            mIsCreatingNewClient = false;
            InitializeComponent();
            this.mSaveButton.Visible = true;
            this.mCreateButton.Visible = false;
            PopulateClient();
        }

        /// <summary>
        /// Constructor, given a client ID
        /// </summary>
        /// <param name="clientID"></param>
        public FrmViewClient(long clientID)
        {
            mIsCreatingNewClient = false;
            InitializeComponent();
            this.mSaveButton.Visible = true;
            this.mCreateButton.Visible = false;
            PopulateClientFromID(clientID);
        }

        /// <summary>
        /// Populates the Client Form using a Client ID
        /// </summary>
        /// <param name="clientID"></param>
        private void PopulateClientFromID(long clientID)
        {
            mClient = Client.GetClientByID(clientID);
            mClient.SetUserID(clientID);
            FillBasicClientInfo(mClient);

        }

        /// <summary>
        /// Fill in the Client Form with data in PopulateClientFromID
        /// </summary>
        /// <param name="client"></param>
        public void FillBasicClientInfo(Client matchingClient)
        {
            mClientID.Text = matchingClient.GetClientId().ToString();
            if (matchingClient.GetFirstName() != null)
                mFirstName.Text = matchingClient.GetFirstName();
            if (matchingClient.GetMiddleName() != null)
                mMiddleName.Text = matchingClient.GetMiddleName();
            if (matchingClient.GetLastName() != null)
                mLastName.Text = matchingClient.GetLastName();
            if (matchingClient.GetDateOfBirth().ToString() != null)
                mDOB.Text = matchingClient.GetDateOfBirth().ToShortDateString();
            if (matchingClient.GetPhoneNumber1() != null)
                mPhone1.Text = matchingClient.GetPhoneNumber1();
            if (matchingClient.GetPhoneNumber2() != null)
                mPhone2.Text = matchingClient.GetPhoneNumber2();
            if (matchingClient.GetAddress1() != null)
                mAddress1.Text = matchingClient.GetAddress1();
            if (matchingClient.GetAddress2() != null)
                mAddress2.Text = matchingClient.GetAddress2();
            if (matchingClient.GetCity() != null)
                mCity.Text = matchingClient.GetCity();
            if (matchingClient.GetState() != null)
                mState.Text = matchingClient.GetState();
            if (matchingClient.GetZipCode() != null)
                mZip.Text = matchingClient.GetZipCode();
            if (matchingClient.GetEmail() != null)
                mEmail.Text = matchingClient.GetEmail();

        }

        /// <summary>
        /// Populates the Client Form with data from the 
        /// </summary>
        /// <param name="client"></param>
        public void PopulateClient()
        {
            this.mClientID.Text = mClient.GetClientId().ToString();
            this.mFirstName.Text = mClient.GetFirstName();
            this.mMiddleName.Text = mClient.GetMiddleName();
            this.mLastName.Text = mClient.GetLastName();

            String dob = "";
            DateTime dateOfBirth = mClient.GetDateOfBirth();
            String day = (dateOfBirth.Day < 10) ? "0" + dateOfBirth.Day.ToString() : dateOfBirth.Day.ToString();
            String month = (dateOfBirth.Month < 10) ? "0" + dateOfBirth.Month.ToString() : dateOfBirth.Month.ToString();

            dob = month + "/" + day + "/" + dateOfBirth.Year.ToString();

            this.mDOB.Text = dob;
            this.mPhone1.Text = mClient.GetPhoneNumber1();
            this.mPhone2.Text = mClient.GetPhoneNumber2();
            this.mPhone3.Text = mClient.GetPhoneNumber3();
            this.mAddress1.Text = mClient.GetAddress1();
            this.mAddress2.Text = mClient.GetAddress2();
            this.mSex.Text = mClient.GetSexType();
            this.mCity.Text = mClient.GetCity();
            this.mState.Text = mClient.GetState();
            this.mZip.Text = mClient.GetZipCode();
            this.mEmail.Text = mClient.GetEmail();
            this.mClientAssessment.Text = mClient.GetAssessment().GetAssessmentData();
            this.mClientSummary.Text = mClient.GetSummary().GetSummaryData();

            // build chart tab
            System.Collections.ArrayList charts = mClient.GetAllCharts();
            for (int i = 0; i < charts.Count; ++i)
            {
                Chart chart = (Library.Chart)charts[i];
                String entry = i.ToString();
                String date = chart.GetTimeLastModified().ToShortDateString();
                String author = chart.GetModifyAuthor();

                mClientChart.Rows.Add(entry, date, author);
            }

        }


        /// <summary>
        /// Saves a client to the DataBase
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mSaveButton_Click(object sender, EventArgs e)
        {
            CreateSaveClient();
        }

        /// <summary>
        /// Returns a Client object by pulling data from the form widgets
        /// </summary>
        /// <returns></returns>
        private Client retrieveClientDataFromForms()
        {

            Client client = new Client();

            // populate client data
            client.SetName(this.mFirstName.Text.ToString(),
                            this.mMiddleName.Text.ToString(),
                            this.mLastName.Text.ToString());
            client.SetDateOfBirth(Convert.ToDateTime(mDOB.Text));
            client.SetEmail(mEmail.Text.ToString());
            client.SetPhoneNumber1(mPhone1.Text.ToString());
            client.SetPhoneNumber2(mPhone2.Text.ToString());
            client.SetPhoneNumber3(mPhone3.Text.ToString());
            client.SetAddress1(mAddress1.Text.ToString());
            client.SetAddress2(mAddress2.Text.ToString());
            client.SetCity(mCity.Text.ToString());
            client.SetState(mState.Text.ToString());
            client.SetZipCode(mZip.Text.ToString());

            client.GetAssessment().SetAssessmentData(mClientAssessment.Text.ToString());
            client.GetSummary().SetSummaryData(mClientSummary.Text.ToString());

            return client;
        }

        /// <summary>
        /// Creates a new Client and saves it in the DataBase
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mCreateButton_Click(object sender, EventArgs e)
        {
            CreateSaveClient();
        }

        /// <summary>
        /// Creates or Saves a Client to the Database
        /// </summary>
        private void CreateSaveClient()
        {
            try
            {
                Client myClient = retrieveClientDataFromForms();
                if (null == myClient)
                {
                    LOG.error("FrmViewClient::mSaveButton_Click: myClient is null");
                    mStatusLbl.Text = "Unexpected Error Occurred";
                    return;
                }

                bool success = false;
                success = Client.SaveClient(myClient);
                
                if (this.mIsCreatingNewClient)
                {
                    if (success)
                    {
                        mStatusLbl.Text = "Client Created and Saved";
                    }
                    else
                    {
                        mStatusLbl.Text = "Error creating client";
                    }
                }
                else
                {
                    if (success)
                    {
                        mStatusLbl.Text = "Client Saved";
                    }
                    else
                    {
                        mStatusLbl.Text = "Error! Client not saved";
                    }
                }
            }
            catch (System.InvalidCastException)
            {
                mStatusLbl.Text = "Invalid Date Format";
            }
            catch (System.FormatException)
            {
                mStatusLbl.Text = "Invalid Date Format";
            }
        }

        /// <summary>
        /// Creates a new chart object
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mChartAddButton_Click(object sender, EventArgs e)
        {
            FrmChart newChart = new FrmChart(mClient, this);
            newChart.Show();

        }

        /// <summary>
        /// Repopulates the Client Chart view grid
        /// </summary>
        public void UpdateClientChartView()
        {

            mClientChart.Rows.Clear();

            // build chart tab
            System.Collections.ArrayList charts = mClient.GetAllCharts();

            for (int i = 0; i < charts.Count; ++i)
            {
                Chart chart = (Library.Chart)charts[i];
                String entry = i.ToString();
                String date = chart.GetTimeLastModified().ToShortDateString();
                String author = chart.GetModifyAuthor();
                mClientChart.Rows.Add(entry, date, author);

            }
        }

        /// <summary>
        /// Launches a Chart form from a cell click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mClientChart_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int entryNumber = 0;

            int selectedRow = mClientChart.SelectedCells[0].RowIndex;
            String szOutput = mClientChart.Rows[selectedRow].Cells[0].Value.ToString();
            entryNumber = int.Parse(szOutput);

            System.Collections.ArrayList charts = mClient.GetAllCharts();
            Chart tempChart = (Chart)charts[entryNumber];

            FrmChart frm = new FrmChart(tempChart, this);
            frm.Show();


        }

        /// <summary>
        /// Reverts form to original input
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mCancelButton_Click(object sender, EventArgs e)
        {
            mStatusLbl.Text = "";
            mDOB.BackColor = Color.White;
            mPhone1.BackColor = Color.White;
            mPhone2.BackColor = Color.White;
            mPhone3.BackColor = Color.White;
            mZip.BackColor = Color.White;
            PopulateClient();
        }

        /// <summary>
        /// Displays invalid alert for DOB
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mDOB_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            mStatusLbl.Text = "Invalid DOB format";
            mDOB.BackColor = Color.Red;
        }

        /// <summary>
        /// Displays invalid alert for phone 1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mPhone1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            mStatusLbl.Text = "Invalid Phone number format";
            mPhone1.BackColor = Color.Red;

        }

        /// <summary>
        /// Displays invalid alert for phone 2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mPhone2_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            mStatusLbl.Text = "Invalid Phone number format";
            mPhone2.BackColor = Color.Red;
        }

        /// <summary>
        /// Displays invalid alert for phone 3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mPhone3_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            mStatusLbl.Text = "Invalid Phone number format";
            mPhone3.BackColor = Color.Red;
        }

        /// <summary>
        /// checks validity of phone 3 field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mPhone3_TextChanged(object sender, EventArgs e)
        {
            if (!mPhone3.MaskCompleted)
            {
                mStatusLbl.Text = "Invalid format for phone 3";
                mPhone3.BackColor = Color.Red;
            }
            else
            {
                mStatusLbl.Text = "";
                mPhone3.BackColor = Color.White;
            }
        }

        /// <summary>
        /// checks validity of phone 2 field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mPhone2_TextChanged(object sender, EventArgs e)
        {
            if (!mPhone2.MaskCompleted)
            {
                mStatusLbl.Text = "Invalid format for phone 2";
                mPhone2.BackColor = Color.Red;
            }
            else
            {
                mStatusLbl.Text = "";
                mPhone2.BackColor = Color.White;
            }
        }

        /// <summary>
        /// checks validity of phone 1 field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mPhone1_TextChanged(object sender, EventArgs e)
        {
            if (!mPhone1.MaskCompleted)
            {
                mStatusLbl.Text = "Invalid format for phone";
                mPhone1.BackColor = Color.Red;
            }
            else
            {
                mStatusLbl.Text = "";
                mPhone1.BackColor = Color.White;
            }
        }

        /// <summary>
        /// checks validity of DOB field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mDOB_TextChanged(object sender, EventArgs e)
        {
            if (!mDOB.MaskCompleted)
            {
                mStatusLbl.Text = "Invalid format for DOB";
                mDOB.BackColor = Color.Red;
            }
            else
            {
                mStatusLbl.Text = "";
                mDOB.BackColor = Color.White;
            }
        }

        /// <summary>
        /// checks validity of address 1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mAddress1_TextChanged(object sender, EventArgs e)
        {
            if (mAddress1.Text == "")
            {
                mStatusLbl.Text = "Need value for Address";
                mAddress1.BackColor = Color.Red;
            }
            else
            {
                mStatusLbl.Text = "";
                mAddress1.BackColor = Color.White;
            }

        }

        /// <summary>
        /// checks validity of city field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mCity_TextChanged(object sender, EventArgs e)
        {
            if (mCity.Text == "")
            {
                mStatusLbl.Text = "Need value for City";
                mCity.BackColor = Color.Red;
            }
            else
            {
                mStatusLbl.Text = "";
                mCity.BackColor = Color.White;
            }

        }

        /// <summary>
        /// checks validity of state field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mState_TextChanged(object sender, EventArgs e)
        {
            if (mState.Text == "")
            {
                mStatusLbl.Text = "Need value for State";
                mState.BackColor = Color.Red;
            }
            else
            {
                mStatusLbl.Text = "";
                mState.BackColor = Color.White;
            }

        }

        /// <summary>
        /// checks validity of first name field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mFirstName_TextChanged(object sender, EventArgs e)
        {
            if (mFirstName.Text == "")
            {
                mStatusLbl.Text = "Need value for First Name";
                mFirstName.BackColor = Color.Red;
            }
            else
            {
                mStatusLbl.Text = "";
                mFirstName.BackColor = Color.White;
            }

        }

        /// <summary>
        /// checks validity of last name field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mLastName_TextChanged(object sender, EventArgs e)
        {
            if (mLastName.Text == "")
            {
                mStatusLbl.Text = "Need value for Last Name";
                mLastName.BackColor = Color.Red;
            }
            else
            {
                mStatusLbl.Text = "";
                mLastName.BackColor = Color.White;
            }
        }

        /// <summary>
        /// Checks validity of sex field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mSex_TextChanged(object sender, EventArgs e)
        {
            if ((mSex.Text.Equals("M")) || (mSex.Text.Equals("F")))
            {
                mStatusLbl.Text = "";
                mSex.BackColor = Color.White;
            }
            else
            {
                mStatusLbl.Text = "Please enter M/F for Sex type";
                mSex.BackColor = Color.Red;
            }
        }


        /// <summary>
        /// Checks inputs to verify correct correctness
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mZip_TextChanged(object sender, EventArgs e)
        {
            if (mZip.MaskCompleted)
            {
                mZip.BackColor = Color.White;
                mStatusLbl.Text = "";
            }
            else
            {
                mZip.BackColor = Color.Red;
                mStatusLbl.Text = "Invalid Zip Code Format";
            }
        }

        /// <summary>
        /// Display invalid alert for zip code
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mZip_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            mStatusLbl.Text = "Invalid Zip Code Entered";
            mZip.BackColor = Color.Red;
        }

        /// <summary>
        /// Populating the View with Client's Appointments in particular dates
        /// </summary>
        private void PopulateClientApptView(DataGridView clientAppointments, Client client, DateTime startViewDate, DateTime endViewDate)
        {
            ArrayList ApptList;
            //Getting the appointments from the Database of a particular client
            ApptList = Library.Appointment.GetAppointmentByClient(client, startViewDate, endViewDate);
            Appointment ApptDetail;

            if (ApptList.Count > 0)
            {
                for (int i = 0; i < ApptList.Count; i++)
                {
                    ApptDetail = (Appointment)ApptList[i];
                    String apptDate = ApptDetail.GetBeginDateTime().ToString("d");
                    String apptTime = ApptDetail.GetBeginTime() + "-" + ApptDetail.GetEndTime();
                    String apptWorker = ApptDetail.GetHcwName(ApptDetail.GetHomeCareWorker());
                    clientAppointments.Rows.Add(apptDate, apptTime, apptWorker);
                }
            }
        }

        /// <summary>
        /// When the User select Appointment Tab from Client Data
        /// It will display the client's appointments for a month
        /// </summary>
        private void mAppointmentTab_Click(object sender, EventArgs e)
        {
            String clientName = mClient.GetFirstName() + " " + mClient.GetLastName();
            clientNameLbl.Text = clientName;
            clientApptView.Rows.Clear();
            //the function to get the client's appointment from today till 30 days
            PopulateClientApptView(clientApptView, mClient, DateTime.Now, DateTime.Now.AddDays(30));
        }

        // used to indicate if we're creating a client or displaying
        // an existing client.
        private bool mIsCreatingNewClient;
        private Client mClient;

    }
}